import { useRouter } from 'next/router';
import React, { useEffect, useRef } from 'react';
import Toast from '../Toast';

type Props = {
  tips?: string;
  path: string;
};
const Redirect: React.FC<Props> = ({ tips, path }) => {
  const router = useRouter();
  const redirectTo = (path: string) => {
    router.push(path);
  };
  const redirectToRef = useRef<any>();
  redirectToRef.current = redirectTo;

  useEffect(() => {
    !tips && redirectToRef.current(path);
  }, [path, tips]);

  return tips ? (
    <Toast message={tips} onClose={() => redirectToRef.current(path)} />
  ) : null;
};

export default Redirect;
